package com.woniu40.petai_user.web;

import com.github.pagehelper.PageInfo;
import com.woniu40.petai_user.entity.Brand;
import com.woniu40.petai_user.entity.Goods;
import com.woniu40.petai_user.entity.GoodsType;
import com.woniu40.petai_user.entity.ProductType;
import com.woniu40.petai_user.result.ResponseResult;
import com.woniu40.petai_user.service.ArticleService;
import com.woniu40.petai_user.service.BrandService;
import com.woniu40.petai_user.service.GoodsService;
import com.woniu40.petai_user.vo.ArticleVo;
import com.woniu40.petai_user.vo.GoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    BrandService brandService;

    @Autowired
    GoodsService goodsService;

    @Autowired
    ArticleService articleService;

    @GetMapping("/findAllBrand")
    @ResponseBody
    public ResponseResult<List<Brand>> findAllBrand(){
        List<Brand> brandList = brandService.findAllBrand();
        ResponseResult<List<Brand>> result = new ResponseResult<>();
        result.setCode(200);
        result.setMessage("查询成功");
        result.setData(brandList);
        return result;
    }

    @GetMapping("/findGoods")
    @ResponseBody
    public ResponseResult<PageInfo<GoodsVo>> findGoodsBySoManyToPage(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                          @RequestParam(value = "pageSize",defaultValue = "12") Integer pageSize,
                                           String productTypeName,
                                           String goodsTypeName,
                                           String goodsName,
                                           String brandName,
                                           BigDecimal minPrice,
                                           BigDecimal maxPrice,
                                           Integer selectId
                                          ) throws ParseException {

        PageInfo<GoodsVo> goodsByPage = goodsService.findGoodsByPage(pageNum, pageSize, productTypeName, goodsTypeName, goodsName, brandName,
                 minPrice, maxPrice,selectId);


        ResponseResult<PageInfo<GoodsVo>> result = new ResponseResult<>();
        result.setCode(200);
        result.setMessage("查询成功");
        result.setData(goodsByPage);
        return result;
    }

    //用于首页展示商品和博客
    @RequestMapping("/show")
    public String goods(Map map,HttpSession session){
        System.out.println(session+"===========");
        List<GoodsVo> goodsList = goodsService.findBySaleCount();
        List<ArticleVo> articleList = articleService.findByCreateTime();
        map.put("goodsList",goodsList);
        map.put("articleList",articleList);
        return "index";
    }




   @RequestMapping("/singleGoods")
    public String findGoodById(Integer goodId, String goodsTypeName,Map map, HttpSession session){
        Goods goodsById = goodsService.findGoodsById(goodId);
        session.setAttribute("goodsId",goodsById.getGoodsId());
        session.setAttribute("goodsName",goodsById.getGoodsName());
        session.setAttribute("salePrice",goodsById.getSalePrice());
        session.setAttribute("cover",goodsById.getCover());
        map.put("Good",goodsById);
        map.put("goodsTypeName",goodsTypeName);
        return "single-product-variable";
    }
}
